package com.transpeed.payservice.pay.wechat;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import com.transpeed.payservice.Utils.PayCommonUtil;
import com.transpeed.payservice.Utils.XMLUtil;
import com.transpeed.payservice.Utils.XmlFormatter;
import com.transpeed.payservice.main.PostMessage;

public class GetWeChatQRCode {	
	
	public static String test_trade_precreate(String TotalAmount ,String Subject ,String NotifyUrl) throws Exception {  
	        // 账号信息  
	        String appid = PayConfigUtil.APP_ID;  // appid  
	        //String appsecret = PayConfigUtil.APP_SECRET; // appsecret  
	        String mch_id = PayConfigUtil.MCH_ID; // 商业号  
	        String key = PayConfigUtil.API_KEY; // key  
	  
	        String currTime = PayCommonUtil.getCurrTime();  
	        String strTime = currTime.substring(8, currTime.length());  
	        String strRandom = PayCommonUtil.buildRandom(4) + "";  
	        String nonce_str = strTime + strRandom;  
	                
	        String order_price = TotalAmount; // 价格   注意：价格的单位是分  
	        String body = Subject;//商品名称
	        
	        String out_trade_no = "WeChat" + System.currentTimeMillis() + (long) (Math.random() * 10000000L);; // 订单号  
	          
	        // 获取发起电脑 ip  
	        String spbill_create_ip = PayConfigUtil.CREATE_IP;  
	        // 回调接口   
	        String notify_url = PayConfigUtil.NOTIFY_URL;  
	        String trade_type = "NATIVE";  
	          
	        SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>();  
	        packageParams.put("appid", appid);  
	        packageParams.put("mch_id", mch_id);  
	        packageParams.put("nonce_str", nonce_str);  
	        packageParams.put("body", body);  
	        packageParams.put("out_trade_no", out_trade_no);  
	        packageParams.put("total_fee", order_price);  
	        packageParams.put("spbill_create_ip", spbill_create_ip);  
	        packageParams.put("notify_url", notify_url);  
	        packageParams.put("trade_type", trade_type);  
	        
	        packageParams.put("attach", NotifyUrl);//附加参数 目前存入上位机通知地址，用于支付成功后，对指定上位机地址推送成功消息
	  
	        String sign = PayCommonUtil.createSign("UTF-8", packageParams,key);  
	        packageParams.put("sign", sign);  
	          
	        String requestXML = PayCommonUtil.getRequestXml(packageParams);  
	        System.out.println(requestXML);  
	        System.out.println(new XmlFormatter().format(requestXML));
	       
	        String resXml = PostMessage.WeChatPost(requestXML , PayConfigUtil.UNIFIEDORDER_URL);  
	  
	          
	        Map map = XMLUtil.doXMLParse(resXml);
	        
	        
	        String urlCode = (String) map.get("code_url");  
	          
	        return urlCode + "&" + out_trade_no;  
	}  

	}

